package com.ruoyi.business.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Book;
import com.ruoyi.business.service.IBookService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约Controller
 * 
 * @author liwenjing
 * @date 2021-05-15
 */
@RestController
@RequestMapping("/business/book")
public class BookController extends BaseController
{
    @Autowired
    private IBookService bookService;

    /**
     * 查询预约列表
     */
    @PreAuthorize("@ss.hasPermi('business:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(Book book)
    {
        startPage();
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 导出预约列表
     */
    @PreAuthorize("@ss.hasPermi('business:book:export')")
    @Log(title = "预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Book book)
    {
        List<Book> list = bookService.selectBookList(book);
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        return util.exportExcel(list, "book");
    }

    /**
     * 获取预约详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bookService.selectBookById(id));
    }

    /**
     * 新增预约
     */
    @PreAuthorize("@ss.hasPermi('business:book:add')")
    @Log(title = "预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Book book)
    {
        return toAjax(bookService.insertBook(book));
    }

    /**
     * 修改预约
     */
    @PreAuthorize("@ss.hasPermi('business:book:edit')")
    @Log(title = "预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Book book)
    {
        return toAjax(bookService.updateBook(book));
    }

    /**
     * 删除预约
     */
    @PreAuthorize("@ss.hasPermi('business:book:remove')")
    @Log(title = "预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bookService.deleteBookByIds(ids));
    }

    /**
     * 获取用户角色
     */
    @GetMapping("/role")
    public SysUser role(){
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        return  loginUser;
    }

    /**
     * 取消预约
     */
    @PreAuthorize("@ss.hasPermi('business:book:cancel')")
    @Log(title = "取消预约",businessType = BusinessType.OTHER)
    @PutMapping(value="/cancel")
    public AjaxResult cancel(@RequestBody Long id){
        return  toAjax(bookService.cancelBook(id));
    }

    /**
     * 确认已到达
     */
    @PreAuthorize("@ss.hasPermi('business:book:confirm')")
    @Log(title = "确认已到达",businessType = BusinessType.OTHER)
    @PutMapping(value="/confirm")
    public AjaxResult confirm(@RequestBody Long id){
        return toAjax(bookService.confirmBook(id));
    }

    /**
     * 获取预约人数
     */
    @GetMapping(value = "/bookNum")
    public Integer bookNum( Book book){
        return bookService.getBookNum(book);
    }
}
